# _*_coding:utf-8_*_
from ATS.instrumentDriver import InstGen


class SMU_B1500A(InstGen.SMU):
    def __init__(self, name, info_dict):
        InstGen.SMU.__init__(self, name, info_dict)
        print(name, info_dict)

    def getmeasrangecode(self, rng):
        """
        rng = (rng_type<0=auto,1=fix,2=limited auto>,rng_val,iv_flag<0=curr,1=volt>)
        """
        if rng[0] == 0:
            return 0
        elif rng[0] == 1:  # fix range
            if rng[2] == 0:  # current measure
                tmp1 = [1e-12, 10e-12, 100e-12, 1e-9, 10e-9, 100e-9, 1e-6, 10e-6, 100e-6, 1e-3, 10e-3, 0.1, 1, 2, 20,
                        40]
                tmp2 = range(-8, -24, -1)
                if tmp1.count(rng[1]) == 0:  # err inval_param
                    return -10010
                else:
                    ind = tmp1.index(rng[1])
                    return tmp2[ind]
            else:  # volt measure
                tmp1 = [0.2, 0.5, 2, 5, 20, 40, 100, 200, 500, 1500, 3000]
                tmp2 = [-2, -5, -11, -50, -12, -13, -14, -15, -5000, -15000, -30000]
                if tmp1.count(rng[1]) == 0:  # err inval_param
                    return -10010
                else:
                    ind = tmp1.index(rng[1])
                    return tmp2[ind]
        elif rng[0] == 2:  # limited auto
            if rng[2] == 0:  # current measure
                tmp1 = [1e-12, 10e-12, 100e-12, 1e-9, 10e-9, 100e-9, 1e-6, 10e-6, 100e-6, 1e-3, 10e-3, 0.1, 1, 2, 20,
                        40]
                tmp2 = range(8, 24, 1)
                if tmp1.count(rng[1]) == 0:  # err inval_param
                    return -10010
                else:
                    ind = tmp1.index(rng[1])
                    return tmp2[ind]
            else:  # volt measure
                tmp1 = [0.2, 0.5, 2, 5, 20, 40, 100, 200, 500, 1500, 3000]
                tmp2 = [2, 5, 11, 50, 12, 13, 14, 15, 5000, 15000, 30000]
                if tmp1.count(rng[1]) == 0:  # err inval_param
                    return -10010
                else:
                    ind = tmp1.index(rng[1])
                    return tmp2[ind]
        else:
            return -10010

    def getsrcrangecode(self, rng):
        """
        rng = (rng_type<0=auto,1=fix,2=best,3=limited_auto>,rng_val,iv_flag<0=curr,1=volt>)
        To B1500, only type 1(fix) and 2(best) are supported, to type 0(auto) the instrument
        will automatically select the best range that covers output value.
        """
        if rng[0] == 0:
            return 0
        elif rng[0] == 3:  # limited auto
            if rng[2] == 0:  # current measure
                tmp1 = [1e-12, 10e-12, 100e-12, 1e-9, 10e-9, 100e-9, 1e-6, 10e-6, 100e-6, 1e-3, 10e-3, 0.1, 1, 2, 20,
                        40]
                tmp2 = range(8, 24, 1)
                if tmp1.count(rng[1]) == 0:  # err inval_param
                    return -10010
                else:
                    ind = tmp1.index(rng[1])
                    return tmp2[ind]
            else:  # volt measure
                tmp1 = [0.2, 0.5, 2, 5, 20, 40, 100, 200, 500, 1500, 3000]
                tmp2 = [2, 5, 11, 50, 12, 13, 14, 15, 5000, 15000, 30000]
                if tmp1.count(rng[1]) == 0:  # err inval_param
                    return -10010
                else:
                    ind = tmp1.index(rng[1])
                    return tmp2[ind]
        else:  # fix, best or other fake value, set to auto
            return 0
